package com.hd.sup.modules.vehicle.model;

import java.io.Serializable;

import java.util.Date;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.baomidou.mybatisplus.annotation.TableName;
import com.hd.common.constant.BizConstant;
import com.hd.common.utils.DateUtil;
import com.hd.common.utils.GenUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

/**
 * <p>
 * 车辆轨迹表 按天分表
 * </p>
 *
 * @author chentl123
 * @since 2020-12-28
 */
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Data
@TableName("sp_vehicle_trail")
public class VehicleTrail extends Model<VehicleTrail> {

    private static final long serialVersionUID = 1L;
    public static final String ERROR = "error";

    //实时轨迹查询构造
    public VehicleTrail(String platNo, String platNoColor) {
        this.platNo = platNo;
        this.platNoColor = platNoColor;
        this.dateSuffix = dateSuffix(new Date());
    }

    //查询构造
    public VehicleTrail(String platNo, String platNoColor, String startDateStr, String endDateStr) {
        this.platNo = platNo;
        this.platNoColor = platNoColor;
        this.startDateStr = startDateStr;
        this.endDateStr = endDateStr;
        this.dateSuffix = dateSuffix(startDateStr);
    }

    //插入构造
    public VehicleTrail(String platNo, String platNoColor, Double longitude, Double latitude, Double speed, Double altitude, Date gpsTime, Double direction, Long alarmType, Long vehicleStatus, Double addSpeed, Integer distance) {
        this.id = GenUtil.newId();
        this.platNo = platNo;
        this.platNoColor = platNoColor;
        this.longitude = longitude;
        this.latitude = latitude;
        this.speed = speed;
        this.altitude = altitude;
        this.gpsTime = gpsTime;
        this.direction = direction;
        this.alarmType = alarmType;
        this.vehicleStatus = vehicleStatus;
        this.addSpeed = addSpeed;
        this.distance = distance;
        this.dateSuffix = dateSuffix(gpsTime);
    }

    /**
     * 车辆轨迹编号
     */
    private String id;
    /**
     * 车牌号
     */
    @TableField("plat_no")
    private String platNo;
    /**
     * 车牌颜色
     */
    @TableField("plat_no_color")
    private String platNoColor;
    /**
     * 经度
     */
    private Double longitude;
    /**
     * 维度
     */
    private Double latitude;
    /**
     * 速度
     */
    private Double speed;
    /**
     * 高度
     */
    private Double altitude;
    /**
     * GPS时间
     */
    @TableField("gps_time")
    private Date gpsTime;
    /**
     * 方向
     */
    private Double direction;
    /**
     * 报警类型 | 整数存二进制
     */
    @TableField("alarm_type")
    private Long alarmType;
    /**
     * 车辆状态 |  整数存二进制  aac 开、aac关等..
     */
    @TableField("vehicle_status")
    private Long vehicleStatus;
    /**
     * 附加信息
     */
    @TableField("info_ex")
    private String infoEx;
    /**
     * 行车记录仪速度
     */
    @TableField("add_speed")
    private Double addSpeed;
    /**
     * 里程
     */
    private Integer distance;

    /**
     * 实时数据标识 0 补报数据 1 实时数据
     */
    @TableField("real_data")
    private Integer realData;

    /**
     * 筛选开始时间
     */
    @TableField(exist = false)
    private String startDateStr;
    /**
     * 筛选结束时间
     */
    @TableField(exist = false)
    private String endDateStr;
    /**
     * 筛选结束时间
     */
    @TableField(exist = false)
    private String dateSuffix;
    /**
     * 导出限制
     */
    @TableField(exist = false)
    private Integer limit;

    private String dateSuffix(String dateStr) {
        if (StringUtils.isBlank(dateStr)) return "";
        Date date = DateUtil.parseTime(dateStr);
        return DateUtil.formatDate(date, BizConstant.Common.DATE_FORMAT_DAY_NO_SEPARATOR.key());
    }

    private String dateSuffix(Date date) {
        if (date == null) return ERROR;
        return DateUtil.format(date, BizConstant.Common.DATE_FORMAT_DAY_NO_SEPARATOR.key());
    }

    @Override
    protected Serializable pkVal() {
        return this.id;
    }
}
